package com.gcq.javawork.controller;

import com.gcq.javawork.common.Result;
import com.gcq.javawork.model.domain.User;
import com.gcq.javawork.model.request.UserRegisteredRequest;
import com.gcq.javawork.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.gcq.javawork.constatnts.BaseMessage.*;
import static com.gcq.javawork.constatnts.UserConstants.COMMON_ROLE;
import static com.gcq.javawork.constatnts.UserConstants.USER_LOGIN_STATUS;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisteredRequest
     * @return
     */
    @PostMapping("/register")
    public Result<Long> registered(@RequestBody UserRegisteredRequest userRegisteredRequest) {
        //逻辑前检测
        if (userRegisteredRequest == null) {
            return Result.fail(LOGIN_MES_CAN_NOT_NULL);
        }
        if (userRegisteredRequest.getUserAccounts() == null || userRegisteredRequest.getUserPassword() == null
                || userRegisteredRequest.getCheckPassword() == null) {
            return Result.fail(LOGIN_MES_CAN_NOT_NULL);
        }
        long id = userService.userRegistered(userRegisteredRequest.getUserAccounts(),
                userRegisteredRequest.getUserPassword(), userRegisteredRequest.getCheckPassword());

        return Result.success(id);
    }

    @GetMapping("/current")
    public Result<User> currentUser(HttpServletRequest request) {
        if (request == null) {
            return Result.fail(40100, USER_NOT_EXIST);
        }
        User current = (User) request.getSession().getAttribute(USER_LOGIN_STATUS);
        User user = userService.getById(current.getId());
        User handledUser = userService.userDesensitization(user);
        return Result.success(handledUser);
    }


    /**
     * 用户登录
     *
     * @param userRegisteredRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/login")
    public Result<User> login(@RequestBody UserRegisteredRequest userRegisteredRequest, HttpServletRequest httpServletRequest) {
        if (userRegisteredRequest == null) {
            return Result.fail(USER_NOT_EXIST);
        }
        if (userRegisteredRequest.getUserAccounts() == null || userRegisteredRequest.getUserPassword() == null
        ) {
            return Result.fail(USER_NOT_EXIST);
        }
        User user = userService.userLogin(userRegisteredRequest.getUserAccounts(),
                userRegisteredRequest.getUserPassword(), httpServletRequest);
        if (user == null) {
            return Result.fail(USER_NOT_EXIST);
        }

        return Result.success(user);
    }

    /**
     * 用户查询
     *
     * @param userName
     * @return
     */
    @GetMapping("/search")
    public Result<List<User>> searchUserByName(String userName, HttpServletRequest httpServletRequest) {
        //查询时候要鉴权
        if (!authentication(httpServletRequest)) {
            return Result.fail(USER_PERMISSION_ERROR);
        }
        if (userName == null) {
            List<User> list = userService.list();
            if (list == null || list.size() == 0) {
                return null;
            }
            return Result.success(list);
        }

        List<User> queryUser = userService.searchUserByName(userName);

        return Result.success(queryUser);
    }

    /**
     * 用户删除
     *
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteUser(long id, HttpServletRequest request) {
        if (!authentication(request)) {
            return Result.fail(USER_PERMISSION_ERROR);
        }
        return Result.success(userService.removeById(id));
    }

    /**
     * 用户登出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<Boolean> logoutUser(HttpServletRequest request) {
        if (request == null) {
            return Result.fail(USER_NOT_EXIST);
        }

        request.getSession().removeAttribute(USER_LOGIN_STATUS);
        return Result.success(true);
    }


    /**
     * 用户鉴权
     *
     * @param request
     * @return
     */
    public boolean authentication(HttpServletRequest request) {
        User user =
                (User) request.getSession().getAttribute(USER_LOGIN_STATUS);
        if (user == null || user.getUserRole().equals(COMMON_ROLE)) {
            return false;
        }
        return true;
    }

}
